package l.l.w.ThinkInJave.Curr;

import java.io.IOException;

/**
 * Created by llw on 2018/3/21/0021.
 */
public class LearnThreadLocal {
    private static int i = 0;
    ThreadLocal<String> threadLocal = new ThreadLocal<String>() {
        @Override
        public String initialValue() {
            return "initial value";
        }
    };
    ThreadLocal<Integer> localInt = new ThreadLocal<Integer>();

    class Thr implements Runnable{

        @Override
        public void run() {
            System.out.println(threadLocal.get());
            threadLocal.set(Thread.currentThread().getName());
            localInt.set(i++);
            System.out.println(threadLocal.get());
            System.out.println(localInt.get());
            try {
                System.in.read();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        LearnThreadLocal learnThreadLocal = new LearnThreadLocal();
        for(int i = 0; i < 10; ++i){
            Thread thread = new Thread(learnThreadLocal.new Thr());
            thread.setName("thread-" + i);
            thread.start();
        }
        System.in.read();
    }
}
